package com.dawnling.datepickerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * PopWindow for Date Pick
 */
public class DatePickerPopWin extends PopupWindow implements OnClickListener {

    public ImageView cancelBtn;
    public Button confirmBtn;
    public View pickerContainerV;
    public View contentView;//root view
    
    public LoopView yearLoopView;

    private int yearPos = 0;
    private Context mContext;
    private OnDatePickedListener mListener;//监听事件

    private List<String> yearList = new ArrayList();
	private TextView tv_datepicker_title;
	public String title;

    public static class Builder {

    	List<String> yearList = new ArrayList();
        //Required
        private Context context;
        private OnDatePickedListener listener;
        public String title;

        public Builder(Context context, OnDatePickedListener listener) {
            this.context = context;
            this.listener = listener;
        }

        public DatePickerPopWin build() {
            return new DatePickerPopWin(this);
        }
        
        public Builder setData(List<String> yearList) {
        	this.yearList = yearList;
        	return this;
        }
        
        public Builder setTitleData(String title) {
        	this.title = title;
			return this;
        }
    }

    public DatePickerPopWin(Builder builder) {
        this.mContext = builder.context;
        this.mListener = builder.listener;
        this.yearList = builder.yearList;
        this.title = builder.title;
        initView();
    }

    private void initView() {
        contentView = LayoutInflater.from(mContext).inflate(R.layout.layout_date_picker_work, null);
        cancelBtn = (ImageView) contentView.findViewById(R.id.btn_cancel);//取消按钮
        confirmBtn = (Button) contentView.findViewById(R.id.btn_confirm);//确定按钮
        tv_datepicker_title = (TextView) contentView.findViewById(R.id.tv_datepicker_title);//
        
        pickerContainerV = contentView.findViewById(R.id.container_picker);
        
        yearLoopView = (LoopView) contentView.findViewById(R.id.picker_year1);

        tv_datepicker_title.setText(title);
        //set checked listen
        yearLoopView.setLoopListener(new LoopScrollListener() {
            @Override
            public void onItemSelect(int item) {
                yearPos = item;
            }
        });

        initPickerViews(); // init year and month loop view

        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        contentView.setOnClickListener(this);

        setTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.FadeInPopWin);
        setContentView(contentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
    }

    /**
     * Init year and month loop view,
     * Let the day loop view be handled separately
     */
    private void initPickerViews() {
    	//TODO 这里初始化数据
        yearLoopView.setDataList((ArrayList) yearList);
        yearLoopView.setInitPosition(yearPos);
    }

    /**
     * Show date picker popWindow
     *
     * @param activity
     */
    public void showPopWin(Activity activity) {

        if (null != activity) {

            TranslateAnimation trans = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                    0, Animation.RELATIVE_TO_SELF, 1,
                    Animation.RELATIVE_TO_SELF, 0);

            showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM,
                    0, 0);
            trans.setDuration(400);
            trans.setInterpolator(new AccelerateDecelerateInterpolator());

            pickerContainerV.startAnimation(trans);
        }
    }

    /**
     * Dismiss date picker popWindow
     */
    public void dismissPopWin() {
        TranslateAnimation trans = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
        trans.setDuration(400);
        trans.setInterpolator(new AccelerateInterpolator());
        trans.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }
        });
        pickerContainerV.startAnimation(trans);
    }

    @Override
    public void onClick(View v) {
    	
        if (v == contentView || v.getId() == R.id.btn_cancel) {
            dismissPopWin();
        } else if (v == confirmBtn) {
            if (null != mListener) {
            	String string = yearList.get(yearPos);
            	mListener.onDatePickCompleted(string, yearPos);
            }
            dismissPopWin();
        }
    }

    public interface OnDatePickedListener {
        void onDatePickCompleted(String year, int yearPos);
    }
}
